package com.wxprogram.smallprogram.controller;

import com.wxprogram.smallprogram.entity.Detail;
import com.wxprogram.smallprogram.entity.FindStudent;
import com.wxprogram.smallprogram.entity.FindTeacher;
import com.wxprogram.smallprogram.entity.utilEntity.PageUtil;
import com.wxprogram.smallprogram.service.ITeacherDeliverReqService;
import com.wxprogram.smallprogram.service.SaveService;
import com.wxprogram.smallprogram.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学长发表
 */
@RestController
@RequestMapping("/findstu/deliver")
public class TeacherDeliverReqController extends BaseController {
    @Autowired
    private ITeacherDeliverReqService iDeliverReqService;
    @Autowired
    private SaveService saveService;

    /**
     * 保存发表信息
     * @param findStudent
     * @return
     */
    @RequestMapping("/save")
    public JSON saveDeliverReq( FindStudent findStudent) {
     int count=iDeliverReqService.insertFindstudent(findStudent);

    if(count>0){
        return JSON.ok("success");
    }else {
        return JSON.error("failure");
    }

    }

    
    /**
     * 学长发表信息
     */
    @GetMapping("/publish")
    public List<FindStudent> DeliverInfo(@RequestParam(name="openID") String openID){
    	return iDeliverReqService.getTeacherPublish(openID);
    }
    /**
     * 学长删除发布信息
     */
    @RequestMapping("/delete")
    public JSON deletePub(@RequestParam(name = "code")int code){
        if (iDeliverReqService.deletePub(code) > 0){
            return JSON.ok("success");
        }else {
            return JSON.error("failure");
        }
    }
    
    /**
     * 按条件获取该类型所有发表
     */
    @GetMapping("/show.action")
    public PageUtil<FindTeacher> getDeliverInfoByMajor(@RequestParam(name="city",required=false,defaultValue="北京市")String city,@RequestParam(name="school",required=false,defaultValue="北京大学")String school,
                                                       @RequestParam(name="majortype",required=false,defaultValue="医学")String majortype,@RequestParam(name="major",required=false)String major,
                                                       @RequestParam(name=("current"),required=false,defaultValue="1")int current){

        return iDeliverReqService.getDeliver(city, school, majortype, major,current);
    }

    /**
     * 获取详细信息
     */
    @GetMapping("/detail")
    public Detail<FindTeacher> getDetail(@RequestParam("openID")String openID,@RequestParam(name = "code")int code){
        Detail<FindTeacher> detail = new Detail<>();
        int isSave = saveService.selectTeacherIsSave(openID,code);
        FindTeacher findTeacher = iDeliverReqService.getDetail(code);
        detail.setIsSave(isSave);
        detail.setResult(findTeacher);
        return detail;
    }

    /**
     * 修改研究生发布查看数
     * @param code
     * @return
     */
    @RequestMapping("/viewnum")
    public JSON updateViewNum(@RequestParam("code") int code){

        int count = iDeliverReqService.updateViewNum(code);
        if(count>0){
            return JSON.ok("success");
        }else {
            return JSON.error("failure");
        }
    }

}
